package com.tinem.platform.module.starter.web.context;

import com.tinem.platform.module.pojo.co.Commons;
import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.pojo.co.RedisKeyEnum;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className ReqContext
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-02 18:21
 */
public class GatewayContext {

    public static StringRedisTemplate stringRedisTemplate;

    public static final ThreadLocal<HttpServletRequest> REQUEST_LOCAL = new ThreadLocal<>();
    public static final ThreadLocal<HttpServletResponse> RESPONSE_LOCAL = new ThreadLocal<>();


    public static HttpServletResponse getResponse() {
        return RESPONSE_LOCAL.get();
    }

    public static HttpServletRequest getRequest() {
        return REQUEST_LOCAL.get();
    }


    public static String getJWT() {
        return REQUEST_LOCAL.get().getHeader(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_JWT);
    }

    public static String getUserId() {
        String reqId = getReq();
        Object v = stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_USER_ID);
        if(v == null){
            return null;
        }
        return v.toString();
    }

    public static String getClient() {
        String reqId = getReq();
        return (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_CLIENT_ID);
    }

    public static String getService() {
        String reqId = getReq();
        return (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_REQ_SERVICE);
    }

    public static String getVersion() {
        String reqId = getReq();
        return (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_REQ_VERSION);
    }

    public static String getMethod() {
        String reqId = getReq();
        return (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_REQ_METHOD);
    }

    public static Long getReqTimestamp() {
        String reqId = getReq();
        String t = (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_REQ_TIMESTAMP);
        return Long.parseLong(t);
    }

    public static Long getReqAcceptTimestamp() {
        String reqId = getReq();
        String t = (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_ACCEPT_TIMESTAMP);
        return Long.parseLong(t);
    }

    public static Long getResTimestamp() {
        String reqId = getReq();
        String t = (String) stringRedisTemplate.opsForHash().get(RedisKeyEnum.gateway_req_info.getKey(reqId), GatewayHeadName.X_PLATFORM_GATEWAY_RES_TIMESTAMP);
        return Long.parseLong(t);
    }
    public static Set<String> getScope() {
        String reqId = getReq();
        return stringRedisTemplate.opsForSet().members(RedisKeyEnum.channel_scope.getKey(reqId));
    }

    public static Boolean isScope(String scope) {
        String reqId = getReq();
        if (stringRedisTemplate.opsForSet().isMember(RedisKeyEnum.channel_scope.getKey(reqId), Commons.ROOT_AUTHORIZE)) {
            return true;
        }
        return stringRedisTemplate.opsForSet().isMember(RedisKeyEnum.channel_scope.getKey(reqId), scope);
    }
    public static String getJwtJit() {
        return getRequest().getHeader(GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_JTI);
    }

    public static String getReq() {
        return getRequest().getHeader(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID);
    }
}
